package cn.luckdou.system.controller.bus_dict;

import java.util.List;
import java.util.Date;

import cn.luckdou.commons.result.PageInfo;
import cn.luckdou.commons.utils.Des3Util;
import cn.luckdou.commons.utils.DictUtils;
import cn.luckdou.commons.utils.JsonUtils;
import cn.luckdou.commons.utils.PinYinUtils;
import cn.luckdou.system.service.bus_dict.IBusDictService;
import cn.luckdou.system.model.bus_dict.BusDict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.luckdou.commons.base.BaseController;
import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;

/**
 * <p>Description:业务字典管理 <／p>
 * Copyright: Copyright (c) 20017;
 * @author Dadou
 * @DateTime 2017-08-08 
 * @version 1.0
 **/
@Controller
@RequestMapping("/admin/busDict")
public class BusDictController extends BaseController {
	// log日志
	private static Logger logger = Logger.getLogger(BusDictController.class);
    
    @Autowired private IBusDictService busDictService;

	/**
	 * <p>Description: shuaxin redis</p>
	 * @author Dadou
	 * @DateTime 2017-08-10
	 * @version 1.0
	 * @param id
	 * @return
	 */
	@PostMapping("/resetRedis")
	@ResponseBody
	public Object resetRedis(String id) {
		BusDict busDict = busDictService.selectById(id);
		boolean b = false;
		if (busDict!=null) b= DictUtils.resetBusDictList(busDict.getType());
		if (b) {
			return renderSuccess("刷新成功！");
		} else {
			return renderError("刷新失败！");
		}
	}
    
    /**
	 * <p>Description: 列表页面</p>
	 * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @version 1.0
     * @return
     */
    @GetMapping("/manager")
    public String manager(Model model) {
		List<BusDict> busDicts = busDictService.selectTypeGroupByType();
		model.addAttribute("busDicts",busDicts);
		return "admin/system/busDict/busDictList";
    }

	/**
	 * <p>Description: 列表分页</p>
	 * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @version 1.0
     * @return
     */
    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(BusDict busDict, Integer page, Integer rows, String sort, String order) {
        EntityWrapper<BusDict> wrapper = new EntityWrapper<BusDict>();
        wrapper.setEntity(busDict);
        Page<BusDict> pages = getPage(page, rows, sort, order);
        pages = busDictService.selectPage(pages,wrapper);
        return pageToPageInfo(pages);
    }
    
    /**
	 * <p>Description: 添加页面</p>
	 * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @version 1.0
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/system/busDict/busDictAdd";
    }
    
    /**
	 * 
	 * <p>Title: pageList</p>
	 * <p>Description: WAP调用分页列表</p>
	 * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @return
	 */
    @PostMapping("/pageList")
    @ResponseBody
    public Page<BusDict> pageList(BusDict busDict,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")  Integer rows,
			@RequestParam(defaultValue = "id") String sort,
			@RequestParam(defaultValue = "desc") String order) {
    	EntityWrapper<BusDict> wrapper = new EntityWrapper<BusDict>();
    	wrapper.setEntity(busDict);
    	Page<BusDict> pages = getPage(page, rows, sort, order);
    	pages = busDictService.selectPage(pages,wrapper);
    	return pages;
    }
    
    /**
	 * 
	 * <p>Title: list</p>
	 * <p>Description: 后台调用列表</p>
	 * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Object list(BusDict busDict) {
		EntityWrapper<BusDict> wrapper = new EntityWrapper<BusDict>();
        wrapper.setEntity(busDict);
		List<BusDict> list = busDictService.selectList(wrapper);
		return list;
	}
	
	/**
	 * 
	 * <p>Title: appList</p>
	 * <p>Description: API接口调用</p>
	 * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @param json
	 * @return
	 */
	@RequestMapping("/app/list")
	@ResponseBody
	public Object appList(BusDict busDict, String json) {
		if (StringUtils.isNotBlank(json)) {
			try {
				json = Des3Util.decode(json);
				json=json.replaceAll("'", "\"");
				busDict= JsonUtils.jsonToPojo(json, BusDict.class);
				EntityWrapper<BusDict> wrapper = new EntityWrapper<BusDict>();
        		wrapper.setEntity(busDict);
				List<BusDict> list = busDictService.selectList(wrapper);
				return success(list.isEmpty()?null:list);
			} catch (Exception e) {
				return error(e.getMessage());
			}
		}
		return error("参数不能为空");
		
	}
	


    /**
	 * <p>Description: 添加</p>
	 * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @version 1.0
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(BusDict busDict, String json) {
		if (StringUtils.isNotBlank(json)) {
			try {
				json = Des3Util.decode(json);
				json=json.replaceAll("'", "\"");
				busDict=JsonUtils.jsonToPojo(json, BusDict.class);
			} catch (Exception e) {
				return error(e.getMessage());
			}
		}
		busDict.setCreatedTime(new Date());
		busDict.setDictPy(PinYinUtils.getSelectPinYin(busDict.getDescription()));
        boolean b = busDictService.insert(busDict);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
	 * <p>Description: 删除</p>
	 * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @version 1.0
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        boolean b = busDictService.deleteById(Long.parseLong(id));
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }
    
    /**
	 * <p>Description: 批量删除</p>
	 * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @version 1.0
	 * @param ids
	 * @return
	 */
	@PostMapping("/deleteIds")
	@ResponseBody
	public Object deleteIds(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			String[] split = ids.split(",");
			int num = 0;
			for (String id : split) {
				boolean b = busDictService.deleteById(Long.parseLong(id));
				if (b) {
					num++;
				}
			}
			if (num > 0) {
				return renderSuccess("删除"+num+"条记录成功！");
			} else {
				return renderError("删除失败！");
			}
		}
		return renderError("参数错误");
	}

    /**
     * 编辑
     * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @version 1.0
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, String id) {
        BusDict busDict = busDictService.selectById(id);
        model.addAttribute("busDict", busDict);
        return "admin/system/busDict/busDictEdit";
    }

    /**
     * 编辑
     * @author Dadou   
	 * @DateTime 2017-08-08 
	 * @version 1.0
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(BusDict busDict, String json) {
		if (StringUtils.isNotBlank(json)) {
			try {
				json = Des3Util.decode(json);
				json=json.replaceAll("'", "\"");
				busDict=JsonUtils.jsonToPojo(json, BusDict.class);
			} catch (Exception e) {
				return error(e.getMessage());
			}
		}
		busDict.setUpdatedTime(new Date());
		busDict.setDictPy(PinYinUtils.getSelectPinYin(busDict.getDescription()));
        boolean b = busDictService.updateById(busDict);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }

	/**
	 * 筛选框初始化
	 * @return
	 */
	@RequestMapping("/busdictlist")
	@ResponseBody
	public List<BusDict>  selectAllBusDict(String  q){
		EntityWrapper<BusDict> wrapper = new EntityWrapper<>();
		wrapper.or();
		wrapper.like("description",q);
		wrapper.or();
		wrapper.like("dict_py",q);
		wrapper.groupBy("type");
		List<BusDict> busDicts = busDictService.selectList(wrapper);
		return  busDicts;
	}
    
}
